import os
from PySide2.QtWidgets import QApplication
from PySide2.QtUiTools import QUiLoader
from requests_html import HTMLSession

class Baidu_spider:
    def __init__(self):



        self.session = HTMLSession()
        self.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)"}

        self.file = ''
        self.pic_num = 0
        # 从 UI 定义中动态 创建一个相应的窗口对象
        # 注意：里面的控件对象也成为窗口对象的属性了
        # 比如 self.ui.button , self.ui.textEdit
        self.ui = QUiLoader().load('baidu_download_images.ui') # 导入UI文件，self.ui作为实例对象
        self.ui.button1.clicked.connect(self.get_all_num)
        # if (self.ui.button1.clicked.connect(self.get_all_num)):
        #     self.ui.infomation.appendPlainText('正在检测图片总数，请稍等.....')
        self.ui.button2.clicked.connect(self.run)


    def get_html(self, url):
        r = self.session.get(url, headers=self.headers, timeout=7)
        return r

    def get_all_num(self):
        s = 0
        print('正在检测图片总数，请稍等.....')
        images_Lists = self.parse_html(2000) # 得到的是迭代器，每一个元素是一个列表
        for images_list in images_Lists:
            s+=len(images_list)
        print("经检测{}的图片有{}张".format(self.word,s))

    def parse_html(self,restrict_num):
        t = 0
        self.word = self.ui.input_text.toPlainText()
        url  = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + self.word + '&pn='
        while t < restrict_num:
            Url = url + str(t)
            try:
                r = self.get_html(Url)
            except BaseException:
                t = t + 60
                continue
            else:
                pic_url_list = r.html.search_all('"objURL":"{}",')
                if len(pic_url_list) == 0:
                    break
                else:
                    yield pic_url_list
                    t = t + 60

    def create_file(self):
        self.file =  self.ui.input_text.toPlainText()
        y = os.path.exists(self.file)
        if y == 1:
            print('该文件已存在，请重新输入')
            self.file = input('请建立一个存储图片的文件夹，)输入文件夹名称即可')
            os.mkdir(self.file)
        else:
            os.mkdir(self.file)

    def download_images(self,url_list):
        # print('找到关键词:' + self.word + '的图片，即将开始下载图片...')
        self.ui.infomation.appendPlainText('找到关键词:' + self.word + '的图片，即将开始下载图片...')
        for url in url_list:
            QApplication.processEvents()
            self.ui.infomation.appendPlainText('正在下载{}的第'.format(self.word) + str(self.pic_num + 1) + '张图片')
            try:
                if url is not None:
                    pic = self.get_html(url[0])
                else:
                    continue
            except BaseException:
                self.ui.infomation.appendPlainText('错误，当前图片无法下载')
                continue
            else:
                string = self.file + r'\\' + self.word + '_' + str(self.pic_num) + '.jpg'
                fp = open(string, 'wb')
                fp.write(pic.content)
                fp.close()
                self.pic_num += 1
            if self.pic_num >= self.total_number:
                return
    

    def run(self):
        self.total_number = self.ui.input_num.value()
        self.create_file()
        images_lists = self.parse_html(self.total_number)
        for image_list in images_lists:
            self.download_images(image_list)
        self.ui.infomation.appendPlainText("{}张图片下载完毕！".format(self.total_number))





if __name__ == '__main__':
    app = QApplication([])
    stats = Baidu_spider()
    stats.ui.show()
    app.exec_()